package com.gsafety.library.ViewModels;

import android.animation.Animator;
import android.view.animation.Animation;

import androidx.annotation.NonNull;
import androidx.lifecycle.Lifecycle;
import androidx.lifecycle.LifecycleEventObserver;
import androidx.lifecycle.LifecycleOwner;
import androidx.lifecycle.ViewModel;

import java.util.Iterator;
import java.util.LinkedList;

/**
 * 管理在生命周期内的动画，当界面被销毁，则自动取消动画
 * 如果动画支持暂停和恢复，那么也会根据生命周期自动 暂停/恢复。
 */
public class AnimLifeViewModel extends ViewModel implements LifecycleEventObserver
{
    // 动画队列
    private LinkedList<Animation> animations = new LinkedList<>();
    private LinkedList<Animator> animators = new LinkedList<>();
    // 需要恢复的动画队列
    private LinkedList<Animator> resumeAnimators = new LinkedList<>();

    /**
     * 启动动画
     */
    public void start(Animation animation)
    {
        animation.start();
        animations.offer(animation);
    }

    /**
     * 启动动画
     */
    public void start(Animator animator)
    {
        animator.start();
        animators.offer(animator);
    }

    /**
     * 移除
     */
    public void remove(Animator animator)
    {
        animators.remove(animator);
    }

    /**
     * 移除
     */
    public void remove(Animation animation)
    {
        animations.remove(animation);
    }

    /**
     * 结尾清理工作
     */
    @Override
    protected void onCleared()
    {
        Iterator<Animation> animationIterator = animations.iterator();
        while (animationIterator.hasNext())
        {
            Animation animation = animationIterator.next();
            animationIterator.remove();
            if (animation != null)
            {
                animation.cancel();
            }
        }
        Iterator<Animator> animatorIterator = animators.iterator();
        while (animatorIterator.hasNext())
        {
            Animator animator = animatorIterator.next();
            animatorIterator.remove();
            if (animator != null && animator.isRunning())
            {
                animator.cancel();

                animator.pause();
                animator.resume();
            }
        }
        super.onCleared();
    }

    @Override
    public void onStateChanged(@NonNull LifecycleOwner source, @NonNull Lifecycle.Event event)
    {
        switch (event)
        {
            case ON_PAUSE:
            {
                Iterator<Animator> animatorIterator = animators.iterator();
                while (animatorIterator.hasNext())
                {
                    Animator animator = animatorIterator.next();
                    if (animator.isRunning())
                    {
                        animator.pause();
                        resumeAnimators.offer(animator);
                    }
                }
            }
            break;
            case ON_RESUME:
            {
                Iterator<Animator> animatorIterator = resumeAnimators.iterator();
                while (animatorIterator.hasNext())
                {
                    Animator animator = animatorIterator.next();
                    animatorIterator.remove();
                    animator.resume();
                }
            }
            break;
            default:
                break;
        }

    }
}
